package com.cn.springcloudshop.service.impl;

import com.cn.springcloudshop.mapper.YmShopProductMapper;
import com.cn.springcloudshop.model.YmShopProduct;
import com.cn.springcloudshop.model.YmShopProductExample;
import com.cn.springcloudshop.service.inter.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.Date;
import java.util.List;

/**
 * @Author: wuyanzu
 * @Date: 2018/7/30
 */
@Service("ProductService")
@Transactional(rollbackFor = Exception.class)
public class ProductServiceImpl implements ProductService {

    @Autowired
    YmShopProductMapper ymShopProductMapper;

    @Override
    public int countByExample(YmShopProductExample example) {
        Assert.notNull(example, "商品实例不能为空");
        Integer res = ymShopProductMapper.countByExample(example);
        return res == null ? 0 : res;
    }

    @Override
    public int deleteByPrimaryKey(Long productId) {
        Assert.notNull(productId,"productId不能为空");
        Integer res = ymShopProductMapper.deleteByPrimaryKey(productId);
        return res == null ? 0 : res;
    }

    @Override
    public int insert(YmShopProduct record) {
        Assert.notNull(record,"商品不能为空");
        record.setCreateTime(new Date());
        record.setSalesVolume(0L);
        Integer res = ymShopProductMapper.insert(record);
        return res == null ? 0 : res;
    }

    @Override
    public int insertSelective(YmShopProduct record) {
        Assert.notNull(record,"商品不能为空");
        Integer res = ymShopProductMapper.insertSelective(record);
        return res == null ? 0 : res;
    }

    @Override
    public YmShopProduct selectByPrimaryKey(Long productId) {
        Assert.notNull(productId,"productId不能为空");
        YmShopProduct ymShopProduct = ymShopProductMapper.selectByPrimaryKey(productId);
        return ymShopProduct == null ? null : ymShopProduct;
    }

    @Override
    public int updateByPrimaryKeySelective(YmShopProduct record) {
        Assert.notNull(record,"商品不能为空");
        record.setUpdateTime(new Date());
        Integer res = ymShopProductMapper.updateByPrimaryKeySelective(record);
        return res == null ? 0 : res;
    }

    @Override
    public int updateByPrimaryKey(YmShopProduct record) {
        Assert.notNull(record,"商品不能为空");
        record.setUpdateTime(new Date());
        Integer res = ymShopProductMapper.updateByPrimaryKey(record);
        return res == null ? 0 : res;
    }

    @Override
    public List<YmShopProduct> selectByExample(YmShopProductExample example) {
        Assert.notNull(example,"商品实例不能为空");
        return ymShopProductMapper.selectByExample(example);
    }

    @Override
    public int updateBatchByPK(List<YmShopProduct> products) {
        Assert.notEmpty(products,"商品不能为空");
        Integer res = ymShopProductMapper.updateBatchByPK(products);
        return res == null ? 0 : res;
    }

    @Override
    public List<YmShopProduct> selectListByPrimaryKey(List<Long> ids) {
        Assert.notEmpty(ids, "商品id不能为空");
        return ymShopProductMapper.selectListByPrimaryKey(ids);
    }
}
